/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.example;

import java.util.Properties;

import org.sss.client.Job;
import org.sss.client.JobEngine;
import org.sss.client.SssClient;
import org.sss.mapreduce.Configuration;
import org.sss.mapreduce.GroupID;

public class ContMergeReduceTestApp {
  private static void usage() {
    System.err.println("Usage:");
    System.err.println("java ContMergeReduceTestApp gid0 gid1 size sec");
    System.err.println("  gid0: input group id");
    System.err.println("  gid1: input group id");
    System.err.println("  size: MergeReducer's internal buffer size");
    System.err.println("   sec: MergeReducer will be invoked in every sec seconds");
    System.exit(1);
  }
  /**
   * Usage:
   * java ContMergeReduceTestApp
   */
  public static void main(String[] args) throws Exception {
    SssClient client = new SssClient(args);
    String[] others = client.getArgs();
    if (others.length != 4) {
      usage();
    }

    JobEngine engine = new JobEngine(client);

    GroupID i0 = GroupID.createFromString(others[0]);
    GroupID i1 = GroupID.createFromString(others[1]);
    GroupID out = GroupID.createRandom(engine);
    Job.Builder builder =
      engine.getJobBuilder("cmr", ContMergeReduceTest.class)
      .addInput(i0).addInput(i1).addOutput(out);
    Configuration conf = builder.getConfiguration();
    conf.setInt("buffer.size", Integer.parseInt(others[2]));
    builder.build();

    engine.setInterval(Long.parseLong(others[3]) * 1000L);
    Properties execInfo = engine.contExec();

    System.out.println("taskSetID - " + execInfo.getProperty("job.taskset.id"));
    System.out.println("jobID [cmr] - " + execInfo.getProperty("job.cmr.id"));
    System.out.println("output tuples from cmr will be deployed - " + out);

    try {
      while (true) {
        Thread.sleep(1000L);
      }
    }
    catch (InterruptedException e) {
      engine.stopContExec(false);
    }
  }
}
